<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class mis_print_excel extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();

		$this->secure_session();
	}
	
	function laporan_perangkat()
	{
		
		$x=0;

		$ls_sql	= "	SELECT 		b.id_perangkat,b.KODE_BARANG,b.kondisi_barang,b.NAMA_BARANG,b.NAMA_MEREK,b.SPESIFIKASI,b.TIPE,b.HARGA,b.JUMLAH,b.KETERANGAN
					FROM 		ADMIN_PRIMKOKAS.MIS_BARANG b
					WHERE		(UPPER(b.id_perangkat) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(b.KODE_BARANG) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(b.NAMA_BARANG) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(b.NAMA_MEREK) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(b.SPESIFIKASI) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(b.TIPE) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(b.HARGA) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(b.JUMLAH) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(b.KETERANGAN) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'b.KODE_BARANG' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		$ls_sql2	= "	SELECT COUNT(1)
						FROM 		ADMIN_PRIMKOKAS.MIS_BARANG b
						WHERE		(UPPER(b.id_perangkat) LIKE '%".strtoupper(post('keyword'))."%'
									OR UPPER(b.KODE_BARANG) LIKE '%".strtoupper(post('keyword'))."%'
									OR UPPER(b.NAMA_BARANG) LIKE '%".strtoupper(post('keyword'))."%'
									OR UPPER(b.NAMA_MEREK) LIKE '%".strtoupper(post('keyword'))."%'
									OR UPPER(b.SPESIFIKASI) LIKE '%".strtoupper(post('keyword'))."%'
									OR UPPER(b.TIPE) LIKE '%".strtoupper(post('keyword'))."%'
									OR UPPER(b.HARGA) LIKE '%".strtoupper(post('keyword'))."%'
									OR UPPER(b.JUMLAH) LIKE '%".strtoupper(post('keyword'))."%'
									OR UPPER(b.KETERANGAN) LIKE '%".strtoupper(post('keyword'))."%')
						ORDER BY ".(post('sort')=='' ? 'b.KODE_BARANG' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
						
		$total = $this->get_data($ls_sql2);

		$result = $this->db->query($ls_sql);

		$data_p = array(	'rows'=> $result->result_array(), 
								'total' => $total);

		$date = date('Y-m-d');

		//load our new PHPExcel library
		$this->load->library('excel');

		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle("Laporan Perangkat_".$date);

		$no = 1;

			
		if ($data_p) {

			$styleArrayBorder = array(
				'borders' => array(
					'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				)
			);

			$this->excel->getActiveSheet()->setCellValue('B8', 'NO');
			$this->excel->getActiveSheet()->setCellValue('C8', 'KODE BARANG');
			$this->excel->getActiveSheet()->setCellValue('D8', 'NAMA BARANG');
			$this->excel->getActiveSheet()->setCellValue('E8', 'NAMA MERK');
			$this->excel->getActiveSheet()->setCellValue('F8', 'SPESIFIKASI');
			$this->excel->getActiveSheet()->setCellValue('G8', 'KONDISI BARANG');
			$this->excel->getActiveSheet()->setCellValue('H8', 'TIPE');
			$this->excel->getActiveSheet()->setCellValue('I8', 'HARGA');
			$this->excel->getActiveSheet()->setCellValue('J8', 'JUMLAH');
			$this->excel->getActiveSheet()->setCellValue('K8', 'KETERANGAN');

			$this->excel->getActiveSheet()->getStyle('B8:K8')->getFont()->setBold(true);
			$this->excel->getActiveSheet()->getStyle('B8:K8')->getFill()->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID,'startcolor' => array('rgb' => 'EEEE11')));
			$this->excel->getActiveSheet()->getStyle('B8:K8')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

			$x = 8;

			for($i=0;$i<=$data_p['total']-1;$i++){
				$x++;

				$rows = $data_p['rows'];
				$rows2 = $rows[$i];

				$this->excel->getActiveSheet()->setCellValue('B'.$x, $no);
				$this->excel->getActiveSheet()->setCellValue('C'.$x, $rows2['kode_barang']);
				$this->excel->getActiveSheet()->setCellValue('D'.$x, $rows2['nama_barang']);
				$this->excel->getActiveSheet()->setCellValue('E'.$x, $rows2['nama_merek']);
				$this->excel->getActiveSheet()->setCellValue('F'.$x, $rows2['spesifikasi']);
				$this->excel->getActiveSheet()->setCellValue('G'.$x, $rows2['kondisi_barang']);
				$this->excel->getActiveSheet()->setCellValue('H'.$x, $rows2['tipe']);
				$this->excel->getActiveSheet()->setCellValue('I'.$x, $rows2['harga']);
				$this->excel->getActiveSheet()->getStyle('I'.$x)->getNumberFormat()->setFormatCode('Rp #,##0');
				$this->excel->getActiveSheet()->setCellValue('J'.$x, $rows2['jumlah']);
				$this->excel->getActiveSheet()->setCellValue('K'.$x, $rows2['keterangan']);

				$no++;
			}

			
		}

		$this->excel->getActiveSheet()->setCellValue('B4', strtoupper("Laporan Perangkat"));
		$this->excel->getActiveSheet()->setCellValue('B5', '('.$date.')');			

		$styleArray = array(
			'borders' => array(
			    'allborders' => array(
			    'style' => PHPExcel_Style_Border::BORDER_THIN
			    )
			),
			'font'  => array(
			    'size'  => 9,
			    'name'  => 'verdana'
			)
		);

		$this->excel->getActiveSheet()->getStyle('B8:K'.$x)->applyFromArray($styleArray);
		unset($styleArray);

		$styleArray1 = array(
		    'font'  => array(
		    'bold'  => true,
		    'uppercase' => true,
		    'size'  => 11,
		    'name'  => 'Calibri'
		    )
		);

		$this->excel->getActiveSheet()->getColumnDimension('C')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('D')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
		$this->excel->getActiveSheet()->getColumnDimension('F')->setWidth(60);
		$this->excel->getActiveSheet()->getColumnDimension('G')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
		$this->excel->getActiveSheet()->getColumnDimension('I')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('J')->setWidth(15);
		$this->excel->getActiveSheet()->getColumnDimension('K')->setWidth(35);
		

		//$this->excel->getActiveSheet()->getStyle('D1:K'.$this->excel->getActiveSheet()->getHighestRow())->getNumberFormat()->setFormatCode('#,##0.00'); 

		$this->excel->getActiveSheet()->getStyle('C1:C'.$this->excel->getActiveSheet()->getHighestRow())
    	->getAlignment()->setWrapText(true);
    	$this->excel->getActiveSheet()->getStyle('K1:K'.$this->excel->getActiveSheet()->getHighestRow())
    	->getAlignment()->setWrapText(true);

		//set aligment to center for that merged cell (A1 to D1)
		// $this->excel->getActiveSheet()->getStyle('B1:B2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->excel->getActiveSheet()->getStyle('B4:B5')->getFont()->setBold(true);

		$filename= 'Laporan Perangkat_'.$date.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			             
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

	function stock_persediaan_perangkat()
	{
		
		$x=0;

		$ls_sql	= " SELECT	*
					FROM	mis_barang
					WHERE	(UPPER(kode_barang) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_barang) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_merek) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(spesifikasi) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(tipe) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(harga) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(jumlah) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(keterangan) LIKE '%".strtoupper(post('keyword'))."%')
							ORDER BY ".(post('sort')=='' ? 'kode_barang' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
	
		$ls_sql2	= "	SELECT COUNT(1)
					FROM 	mis_barang
					WHERE (UPPER(kode_barang) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_barang) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_merek) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(spesifikasi) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(tipe) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(harga) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(jumlah) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(keterangan) LIKE '%".strtoupper(post('keyword'))."%')
							ORDER BY ".(post('sort')=='' ? 'kode_barang' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		
		$total = $this->get_data($ls_sql2);

		$result = $this->db->query($ls_sql);

		$data_p = array(	'rows'=> $result->result_array(), 
								'total' => $total);

		$date = date('Y-m-d');

		//load our new PHPExcel library
		$this->load->library('excel');
		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle("Stock Persediaan Perangkat");

		$no = 1;

			
		if ($data_p) {

			$styleArrayBorder = array(
				'borders' => array(
					'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				)
			);

			$this->excel->getActiveSheet()->setCellValue('B8', 'NO');
			$this->excel->getActiveSheet()->setCellValue('C8', 'KODE BARANG');
			$this->excel->getActiveSheet()->setCellValue('D8', 'NAMA BARANG');
			$this->excel->getActiveSheet()->setCellValue('E8', 'NAMA MEREK');
			$this->excel->getActiveSheet()->setCellValue('F8', 'SPESIFIKASI');
			$this->excel->getActiveSheet()->setCellValue('G8', 'TIPE');
			$this->excel->getActiveSheet()->setCellValue('H8', 'HARGA');
			$this->excel->getActiveSheet()->setCellValue('I8', 'STOCK PERSEDIAAN');
			$this->excel->getActiveSheet()->setCellValue('J8', 'KETERANGAN');
			
			$this->excel->getActiveSheet()->getStyle('B8:J8')->getFont()->setBold(true);
			$this->excel->getActiveSheet()->getStyle('B8:J8')->getFill()->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID,'startcolor' => array('rgb' => 'EEEE11')));
			$this->excel->getActiveSheet()->getStyle('B8:J8')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

			$x = 8;

			for($i=0;$i<=$data_p['total']-1;$i++){
				$x++;

				$rows = $data_p['rows'];
				$rows2 = $rows[$i];

				$this->excel->getActiveSheet()->setCellValue('B'.$x, $no);
				$this->excel->getActiveSheet()->setCellValue('C'.$x, $rows2['kode_barang']);
				$this->excel->getActiveSheet()->setCellValue('D'.$x, $rows2['nama_barang']);
				$this->excel->getActiveSheet()->setCellValue('E'.$x, $rows2['nama_merek']);
				$this->excel->getActiveSheet()->setCellValue('F'.$x, $rows2['spesifikasi']);
				$this->excel->getActiveSheet()->setCellValue('G'.$x, $rows2['tipe']);
				$this->excel->getActiveSheet()->setCellValue('H'.$x, $rows2['harga']);
				$this->excel->getActiveSheet()->getStyle('H'.$x)->getNumberFormat()->setFormatCode('Rp #,##0');
				$this->excel->getActiveSheet()->setCellValue('I'.$x, $rows2['stock_persediaan']);
				$this->excel->getActiveSheet()->getStyle('I'.$x)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
				$this->excel->getActiveSheet()->setCellValue('J'.$x, $rows2['keterangan']);
				$no++;
			}
		}

		$this->excel->getActiveSheet()->setCellValue('B4', strtoupper("Stock Perangkat"));
		$this->excel->getActiveSheet()->setCellValue('B5', '('.$date.')');			

		$styleArray = array(
			'borders' => array(
			    'allborders' => array(
			    'style' => PHPExcel_Style_Border::BORDER_THIN
			    )
			),
			'font'  => array(
			    'size'  => 9,
			    'name'  => 'verdana'
			)
		);

		$this->excel->getActiveSheet()->getStyle('B8:J'.$x)->applyFromArray($styleArray);
		unset($styleArray);

		$styleArray1 = array(
		    'font'  => array(
		    'bold'  => true,
		    'uppercase' => true,
		    'size'  => 11,
		    'name'  => 'Calibri'
		    )
		);

		$this->excel->getActiveSheet()->getColumnDimension('C')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
		$this->excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
		$this->excel->getActiveSheet()->getColumnDimension('F')->setWidth(60);
		$this->excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
		$this->excel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
		$this->excel->getActiveSheet()->getColumnDimension('I')->setWidth(30);
		$this->excel->getActiveSheet()->getColumnDimension('J')->setWidth(35);

		//$this->excel->getActiveSheet()->getStyle('D1:J'.$this->excel->getActiveSheet()->getHighestRow())->getNumberFormat()->setFormatCode('#,##0.00'); 

		$this->excel->getActiveSheet()->getStyle('C1:C'.$this->excel->getActiveSheet()->getHighestRow())
    	->getAlignment()->setWrapText(true);
    	$this->excel->getActiveSheet()->getStyle('J1:J'.$this->excel->getActiveSheet()->getHighestRow())
    	->getAlignment()->setWrapText(true);

		//set aligment to center for that merged cell (A1 to D1)
		// $this->excel->getActiveSheet()->getStyle('B1:B2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->excel->getActiveSheet()->getStyle('B4:B5')->getFont()->setBold(true);

		$filename= 'Stock Perangkat'.$date.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			             
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

	function komplain()
	{
		
		$x=0;

		$ls_sql	= " SELECT	a.*,b.nama_departemen
					FROM	mis_komplain a 
					LEFT JOIN hrd_departemen b ON a.nama_unit = b.id_departemen_pk
					WHERE	(UPPER(a.kode_komplain) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(b.nama_departemen) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.keterangan) LIKE '%".strtoupper(post('keyword'))."%') 
							ORDER BY ".(post('sort')=='' ? 'a.kode_komplain' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		
		$ls_sql2	= "	SELECT COUNT(1)
						FROM	mis_komplain a 
						LEFT JOIN hrd_departemen b ON a.nama_unit = b.id_departemen_pk
						WHERE	(UPPER(a.kode_komplain) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(b.nama_departemen) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.keterangan) LIKE '%".strtoupper(post('keyword'))."%') 
							ORDER BY ".(post('sort')=='' ? 'a.kode_komplain' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		
		$total = $this->get_data($ls_sql2);

		$result = $this->db->query($ls_sql);

		$data_p = array(	'rows'=> $result->result_array(), 
								'total' => $total);

		$date = date('Y-m-d');

		//load our new PHPExcel library
		$this->load->library('excel');
		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle("Laporan Komplain_".$date);

		$no = 1;

			
		if ($data_p) {

			$styleArrayBorder = array(
				'borders' => array(
					'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				)
			);

			$this->excel->getActiveSheet()->setCellValue('B8', 'NO');
			$this->excel->getActiveSheet()->setCellValue('C8', 'KODE KOMPLAIN');
			$this->excel->getActiveSheet()->setCellValue('D8', 'TGL KOMPLAIN');
			$this->excel->getActiveSheet()->setCellValue('E8', 'DEPARTEMEN');
			$this->excel->getActiveSheet()->setCellValue('F8', 'KOMPLAIN');
			$this->excel->getActiveSheet()->setCellValue('G8', 'STATUS');
			$this->excel->getActiveSheet()->setCellValue('H8', 'KETERANGAN');
			
			$this->excel->getActiveSheet()->getStyle('B8:H8')->getFont()->setBold(true);
			$this->excel->getActiveSheet()->getStyle('B8:H8')->getFill()->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID,'startcolor' => array('rgb' => 'EEEE11')));
			$this->excel->getActiveSheet()->getStyle('B8:H8')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

			$x = 8;

			for($i=0;$i<=$data_p['total']-1;$i++){
				$x++;

				$rows = $data_p['rows'];
				$rows2 = $rows[$i];

				$this->excel->getActiveSheet()->setCellValue('B'.$x, $no);
				$this->excel->getActiveSheet()->setCellValue('C'.$x, $rows2['kode_komplain']);
				$this->excel->getActiveSheet()->setCellValue('D'.$x, $rows2['tgl_komplain']);
				$this->excel->getActiveSheet()->setCellValue('E'.$x, $rows2['nama_departemen']);
				$this->excel->getActiveSheet()->setCellValue('F'.$x, $rows2['komplain']);
				$this->excel->getActiveSheet()->setCellValue('G'.$x, $rows2['status']);
				$this->excel->getActiveSheet()->setCellValue('H'.$x, $rows2['keterangan']);
				$no++;
			}
		}

		$this->excel->getActiveSheet()->setCellValue('B4', strtoupper("Laporan Komplain"));
		$this->excel->getActiveSheet()->setCellValue('B5', '('.$date.')');			

		$styleArray = array(
			'borders' => array(
			    'allborders' => array(
			    'style' => PHPExcel_Style_Border::BORDER_THIN
			    )
			),
			'font'  => array(
			    'size'  => 9,
			    'name'  => 'verdana'
			)
		);

		$this->excel->getActiveSheet()->getStyle('B8:H'.$x)->applyFromArray($styleArray);
		unset($styleArray);

		$styleArray1 = array(
		    'font'  => array(
		    'bold'  => true,
		    'uppercase' => true,
		    'size'  => 11,
		    'name'  => 'Calibri'
		    )
		);

		$this->excel->getActiveSheet()->getColumnDimension('C')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('D')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('E')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('F')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('G')->setWidth(22);
		$this->excel->getActiveSheet()->getColumnDimension('H')->setWidth(40);

		//$this->excel->getActiveSheet()->getStyle('D1:H'.$this->excel->getActiveSheet()->getHighestRow())->getNumberFormat()->setFormatCode('#,##0.00'); 

		$this->excel->getActiveSheet()->getStyle('C1:C'.$this->excel->getActiveSheet()->getHighestRow())
    	->getAlignment()->setWrapText(true);
    	$this->excel->getActiveSheet()->getStyle('H1:H'.$this->excel->getActiveSheet()->getHighestRow())
    	->getAlignment()->setWrapText(true);

		//set aligment to center for that merged cell (A1 to D1)
		// $this->excel->getActiveSheet()->getStyle('B1:B2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->excel->getActiveSheet()->getStyle('B4:B5')->getFont()->setBold(true);

		$filename= 'Laporan Komplain_'.$date.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			             
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

	function terbilang_get_valid($str,$from,$to,$min=1,$max=9){
			$val=false;
			$from=($from<0)?0:$from;
			for ($i=$from;$i<$to;$i++){
				if (((int) $str{$i}>=$min)&&((int) $str{$i}<=$max)) $val=true;
			}
			return $val;
		}	

		function terbilang_get_str($i,$str,$len){
			$numA=array("","Satu","Dua","Tiga","Empat","Lima","Enam","Tujuh","Delapan","Sembilan");
			$numB=array("","Se","Dua ","Tiga ","Empat ","Lima ","Enam ","Tujuh ","Delapan ","Sembilan ");
			$numC=array("","Satu ","Dua ","Tiga ","Empat ","Lima ","Enam ","Tujuh ","Delapan ","Sembilan ");
			$numD=array(0=>"Puluh",1=>"Belas",2=>"Ratus",4=>"Ribu", 7=>"Juta", 10=>"Milyar", 13=>"Triliun");
			$buf="";
			$pos=$len-$i;
			switch($pos){
				case 1:
						if (!$this->terbilang_get_valid($str,$i-1,$i,1,1))
							$buf=$numA[(int) $str{$i}];
					break;
				case 2:	case 5: case 8: case 11: case 14:
						if ((int) $str{$i}==1){
							if ((int) $str{$i+1}==0)
								$buf=($numB[(int) $str{$i}]).($numD[0]);
							else
								$buf=($numB[(int) $str{$i+1}]).($numD[1]);
						}
						else if ((int) $str{$i}>1){
								$buf=($numB[(int) $str{$i}]).($numD[0]);
						}				
					break;
				case 3: case 6: case 9: case 12: case 15:
						if ((int) $str{$i}>0){
								$buf=($numB[(int) $str{$i}]).($numD[2]);
						}
					break;
				case 4: case 7: case 10: case 13:
						if ($this->terbilang_get_valid($str,$i-2,$i)){
							if (!$this->terbilang_get_valid($str,$i-1,$i,1,1))
								$buf=$numC[(int) $str{$i}].($numD[$pos]);
							else
								$buf=$numD[$pos];
						}						else if((int) $str{$i}>0){
							if ($pos==4)
								$buf=($numB[(int) $str{$i}]).($numD[$pos]);
							else
								$buf=($numC[(int) $str{$i}]).($numD[$pos]);
						}
					break;
			}
			return $buf;
		}
		function toTerbilang($nominal){
			$buf="";
			$str=$nominal."";
			$len=strlen($str);
			
			for ($i=0;$i<$len;$i++){
				$buf=trim($buf)." ".$this->terbilang_get_str($i,$str,$len);
			}
			return trim($buf);
		}
}
?>